/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ez430ui.java
 *
 * Created on Jan 2, 2011, 8:59:45 PM
 */
package ui;

import ez430.Watch;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author georg
 */
public class ez430ui extends javax.swing.JFrame {

    private Watch watch = null;
    AccWorker accelerationWorker = null;

    /** Creates new form ez430ui */
    public ez430ui() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtX = new javax.swing.JTextField();
        txtY = new javax.swing.JTextField();
        txtZ = new javax.swing.JTextField();
        btnReadAcc = new javax.swing.JToggleButton();
        txtSerialPort = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        btnStop = new javax.swing.JButton();
        btnStart = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        txtMinY = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtMaxY = new javax.swing.JTextField();
        txtMinX = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtMaxX = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        chkMinY = new javax.swing.JCheckBox();
        chkMinX = new javax.swing.JCheckBox();
        chkMaxX = new javax.swing.JCheckBox();
        chkMaxY = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("ez430 GUI");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setText("X:");

        jLabel2.setText("Y:");

        jLabel3.setText("Z:");

        txtX.setEditable(false);
        txtX.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtX.setDoubleBuffered(true);
        txtX.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtXActionPerformed(evt);
            }
        });

        txtY.setEditable(false);
        txtY.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtY.setDoubleBuffered(true);

        txtZ.setEditable(false);
        txtZ.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtZ.setDoubleBuffered(true);

        btnReadAcc.setText("Detect Gestures");
        btnReadAcc.setEnabled(false);
        btnReadAcc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReadAccActionPerformed(evt);
            }
        });

        txtSerialPort.setText("/dev/ttyACM0");

        jLabel8.setText("Serial Port:");

        btnStop.setText("Stop");
        btnStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStopActionPerformed(evt);
            }
        });

        btnStart.setText("Start");
        btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartActionPerformed(evt);
            }
        });

        jLabel7.setText("Min X:");

        txtMinY.setText("-40");
        txtMinY.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtMinYActionPerformed(evt);
            }
        });

        jLabel6.setText("Min Y:");

        txtMaxY.setText("40");
        txtMaxY.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtMaxYActionPerformed(evt);
            }
        });

        txtMinX.setText("-40");

        jLabel5.setText("Max Y:");

        txtMaxX.setText("40");

        jLabel4.setText("Max X:");

        chkMinY.setEnabled(false);

        chkMinX.setEnabled(false);

        chkMaxX.setEnabled(false);

        chkMaxY.setEnabled(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addGap(18, 18, 18)
                        .addComponent(txtMinX, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(18, 18, 18)
                        .addComponent(txtMinY, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(chkMinY)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(txtMaxY, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(chkMaxY))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(chkMinX)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(txtMaxX, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(chkMaxX)))
                .addContainerGap(21, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel7)
                        .addComponent(txtMinX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(chkMinX))
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(txtMaxX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(chkMaxX))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(chkMaxY)
                    .addComponent(txtMaxY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(chkMinY)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6)
                        .addComponent(txtMinY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(40, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtSerialPort, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnStart)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnStop)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnReadAcc))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(134, 134, 134)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtX, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtY, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtZ, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(132, 132, 132)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(71, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSerialPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(btnStart)
                    .addComponent(btnStop)
                    .addComponent(btnReadAcc))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(txtY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtZ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtXActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtXActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtXActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowOpened

    private void txtMaxYActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtMaxYActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtMaxYActionPerformed

    private void txtMinYActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtMinYActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtMinYActionPerformed

    private void btnReadAccActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReadAccActionPerformed
        if (!btnReadAcc.isSelected()) {
            synchronized (accelerationWorker) {
                if (!accelerationWorker.paused) {
                    accelerationWorker.paused = true;
                }
            }
        } else {
            synchronized (accelerationWorker) {
                accelerationWorker.paused = false;
                accelerationWorker.notify();
            }
        }
    }//GEN-LAST:event_btnReadAccActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
    }//GEN-LAST:event_formWindowClosed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    }//GEN-LAST:event_formWindowClosing

    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed
        watch = new Watch(txtSerialPort.getText());
        txtSerialPort.setEnabled(false);
        btnReadAcc.setEnabled(true);
        btnStart.setEnabled(false);
        accelerationWorker = new AccWorker(this, this.watch);
        accelerationWorker.execute();
    }//GEN-LAST:event_btnStartActionPerformed

    private void btnStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStopActionPerformed
        txtSerialPort.setEnabled(true);
        btnReadAcc.setEnabled(false);
        btnStart.setEnabled(true);

        if (btnReadAcc.isSelected()) {
            if (accelerationWorker != null) {
                if(accelerationWorker.paused == true){
                    synchronized (accelerationWorker) {
                        accelerationWorker.paused = false;
                        accelerationWorker.notify();
                    }
                }
                accelerationWorker.cancel(true);


                accelerationWorker = null;
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ex) {
                    Logger.getLogger(ez430ui.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            btnReadAcc.setSelected(false);
        }

        watch.closePort();
        watch = null;
    }//GEN-LAST:event_btnStopActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ez430ui().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton btnReadAcc;
    private javax.swing.JButton btnStart;
    private javax.swing.JButton btnStop;
    private javax.swing.JCheckBox chkMaxX;
    private javax.swing.JCheckBox chkMaxY;
    private javax.swing.JCheckBox chkMinX;
    private javax.swing.JCheckBox chkMinY;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField txtMaxX;
    private javax.swing.JTextField txtMaxY;
    private javax.swing.JTextField txtMinX;
    private javax.swing.JTextField txtMinY;
    private javax.swing.JTextField txtSerialPort;
    private javax.swing.JTextField txtX;
    private javax.swing.JTextField txtY;
    private javax.swing.JTextField txtZ;
    // End of variables declaration//GEN-END:variables

    /**
     * @param txtX the txtX to set
     */
    public void setTxtX(String txtX) {
        this.txtX.setText(txtX);
    }

    /**
     * @param txtY the txtY to set
     */
    public void setTxtY(String txtX) {
        this.txtY.setText(txtX);
    }

    /**
     * @param txtZ the txtZ to set
     */
    public void setTxtZ(String txtX) {
        this.txtZ.setText(txtX);
    }

    /**
     * @param chkMaxX the chkMaxX to set
     */
    public void setChkMaxX(boolean checked) {
        this.chkMaxX.setSelected(checked);
    }

    /**
     * @param chkMaxY the chkMaxY to set
     */
    public void setChkMaxY(boolean checked) {
        this.chkMaxY.setSelected(checked);
    }

    /**
     * @param chkMinX the chkMinX to set
     */
    public void setChkMinX(boolean checked) {
        this.chkMinX.setSelected(checked);
    }

    /**
     * @param chkMinY the chkMinY to set
     */
    public void setChkMinY(boolean checked) {
        this.chkMinY.setSelected(checked);
    }

    /**
     * @return the txtMaxX
     */
    public String getTxtMaxX() {
        return txtMaxX.getText();
    }

    /**
     * @return the txtMaxY
     */
    public String getTxtMaxY() {
        return txtMaxY.getText();
    }

    /**
     * @return the txtMinX
     */
    public String getTxtMinX() {
        return txtMinX.getText();
    }

    /**
     * @return the txtMinY
     */
    public String getTxtMinY() {
        return txtMinY.getText();
    }
}
